package com.pinyougou.manager.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.pinyougou.pojo.TbBrand;
import com.pinyougou.sellergoods.service.BrandService;
import entity.PageResult;
import entity.Result;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class BrandController {
    @Reference
    private BrandService brandService;

    @RequestMapping("/index")
    public String say(){
        System.out.println("aaa");
        return brandService.say("hello");
    }

    @RequestMapping("/hello")
    public List<TbBrand> findAll(){

        return brandService.findAll();
    }

    @RequestMapping("/findpage")
    public PageResult findpage(int page,int size){

        return brandService.findpage(page,size);
    }


    @RequestMapping("/add")
    public Result add(@RequestBody TbBrand tbBrand){
        try {
            brandService.add(tbBrand);
            System.out.println("success");
            return new Result(true,"add success");

        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"add fail");
        }
    }


    @RequestMapping("/findOne")
    public TbBrand findOne(long id){
        return brandService.findOne(id);
    }

    @RequestMapping("/update")
    public Result update(@RequestBody TbBrand tbBrand){
        try {
            brandService.update(tbBrand);
            System.out.println("success");
            return new Result(true,"update success");

        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"update fail");
        }
    }


    @RequestMapping("/delete")
    public Result delete(long[] ids) {
        try {
            brandService.delete(ids);
            return new Result(true, "delete success");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "delete fail");
        }
    }

    @RequestMapping("/search")
    public PageResult search(@RequestBody TbBrand tbBrand,int page,int size){
        return brandService.findpage(tbBrand,page,size);
    }
}
